package com.yuexunit.finance.member.action.v1;

import java.util.ArrayList;
import java.util.List;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.member.action.v1.model.LogisticsCompanyInfoDetail;
import com.yuexunit.finance.member.entity.LogisticsCompanyInfoEntity;
import com.yuexunit.finance.member.service.LogisticsCompanyInfoService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;

/**
*@version 创建时间：2017年6月12日 下午4:13:20
*@author wenwenlong
*类说明
*/

@Implements(LogisticsCompanyInfoActionApi.class)
public class LogisticsCompanyInfoActionApiImpl implements LogisticsCompanyInfoActionApi{
	private LogisticsCompanyInfoService logisticsCompanyInfoService = ComponentFactory.loadClass(LogisticsCompanyInfoService.class);
	
	@Override
	public String createLogisticsCompanyInfo(String logisticsCompanyName) {
		List<LogisticsCompanyInfoEntity> logisticsCompanyInfoEntityList = logisticsCompanyInfoService.inquireLogisticsCompanyByName(logisticsCompanyName);
		if(logisticsCompanyInfoEntityList != null && logisticsCompanyInfoEntityList.size() > 0) {
			throw new BusinessException("该物流公司已存在，请重新添加");
		}
		LogisticsCompanyInfoEntity entity = new LogisticsCompanyInfoEntity();
		entity.setLogisticsCompanyName(logisticsCompanyName);
		logisticsCompanyInfoService.createLogisticsCompanyInfo(entity);
		return entity.getLogisticsCompanyInfoId().toString();
	}

	@Override
	public void updateLogisticsCompanyInfo(Long logisticsCompanyInfoId, String logisticsCompanyName) {
		LogisticsCompanyInfoEntity entity = logisticsCompanyInfoService.inquireLogisticsCompanyInfo(logisticsCompanyInfoId);
		entity.setLogisticsCompanyName(logisticsCompanyName);
		logisticsCompanyInfoService.updateLogisticsCompanyInfo(entity);
	}

	@Override
	public void disableLogisticsCompanyInfo(Long logisticsCompanyInfoId) {
		if (1L == logisticsCompanyInfoId) {
			throw new BusinessException("该条数据不可被删除！");
		}
		logisticsCompanyInfoService.disableLogisticsCompanyInfo(logisticsCompanyInfoId);
	}

	@Override
	public LogisticsCompanyInfoDetail inquireLogisticsCompanyInfoDetail(Long logisticsCompanyInfoId) {
		LogisticsCompanyInfoEntity entity = logisticsCompanyInfoService.inquireLogisticsCompanyInfo(logisticsCompanyInfoId);
		LogisticsCompanyInfoDetail detail = new LogisticsCompanyInfoDetail();
		BeanUtils.copyProperties(entity, detail);
		return detail;
	}

	@Override
	public List<LogisticsCompanyInfoDetail> inquireLogisticsCompanyInfoList() {
		List<LogisticsCompanyInfoEntity> lcies = logisticsCompanyInfoService.inquireLogisticsCompanyInfoList();
		List<LogisticsCompanyInfoDetail> lcids = new ArrayList<>();
		for (LogisticsCompanyInfoEntity lcie : lcies) {
			LogisticsCompanyInfoDetail lcid = new LogisticsCompanyInfoDetail();
			BeanUtils.copyProperties(lcie, lcid);
			lcids.add(lcid);
		}
		return lcids;
	}

	@Override
	public PageList<LogisticsCompanyInfoDetail> inquireLogisticsCompanyInfoPageList(PageCriteria page) {
		PageList<LogisticsCompanyInfoEntity> lcies = logisticsCompanyInfoService.inquireLogisticsCompanyInfoPageList(page);
		PageList<LogisticsCompanyInfoDetail> lcids = new PageList<>(page, lcies.getTotalRecordCount());
		for (LogisticsCompanyInfoEntity lcie : lcies) {
			LogisticsCompanyInfoDetail lcid = new LogisticsCompanyInfoDetail();
			BeanUtils.copyProperties(lcie, lcid);
			lcids.add(lcid);
		}
		return lcids;
	}
}
